#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/CI/lib -I/data/Software/mydan/CI/private/lib
use strict;
use warnings;
use Logs;
use MIME::Base64;
use Temp;
use MYDan;

$| ++;

=head1 SYNOPSIS

    db => $mysql,
    logs => 日志对象

    #temp uuid buildimage buildscripts projectid imagename
=cut

return sub
{
    my %param = @_;

    my ( $db, $logs, $temp, $uuid, $buildimage, $buildscripts, $projectid, $imagename ) 
        = @param{qw(db logs temp uuid buildimage buildscripts projectid imagename )};

    $logs = Logs->new( 'code.build.merge.makedist.remote' ) unless $logs;

    my $temppath = "$RealBin/../logs/$temp/$uuid";

    return if -d "$temppath/dist";

    $logs->die( "buildscripts null" )  unless  $buildscripts ;
    $logs->die( "buildscripts decode fail" )  unless my $runcont = decode_base64( $buildscripts );

    my $agentauthpath = "/data/glusterfs/dockerimage/$buildimage.key";

    my $run = Temp->new( chmod => 0755 )->dump( $runcont );

    my $remotepath = "/data/open-c3-ci/$uuid";
    print "build path: $imagename\@$remotepath\n";

    map{
        $logs->die( "run build.merge.makedist.remote fail:$!" ) if system $_;
    }(
        "mv '$run' '$temppath/run'",
        "cd '$temppath' && tar -zcvf openc3TempData.tar.gz *",
        "MYDan_Agent_KEY_PATH='$agentauthpath' $MYDan::PATH/dan/tools/rcall -r '$imagename' dump $temppath/openc3TempData.tar.gz --path '$remotepath/openc3TempData.tar.gz'",
        "MYDan_Agent_KEY_PATH='$agentauthpath' $MYDan::PATH/dan/tools/rcall -r '$imagename' exec 'cd $remotepath && tar -m -zxvf openc3TempData.tar.gz &&OPENC3CIPATH='$remotepath' ./run 2>&1 && tar -zcvf dist.tar.gz dist'",
        "MYDan_Agent_KEY_PATH='$agentauthpath' $MYDan::PATH/dan/tools/load -h '$imagename' --sp '$remotepath/dist.tar.gz' --dp '$temppath/dist.tar.gz'",
        "cd '$temppath' && tar -m -zxvf dist.tar.gz",
    );

    $logs->die( "run build.merge.makedist.docker fail:The dist is not a directory" ) unless -d "$temppath/dist";
    $logs->die( "run build.merge.makedist.docker fail:The dist is a soft link" ) if -l "$temppath/dist";
}
